using System;
using System.Data;
using System.Data.SqlClient;
using Framework.Library;
using Framework.Library.Utilities;
using Framework.Module.CMS.Library.Contents;
using Framework.Module.CMS.Library.Distributions;
using Framework.Library.Helper;
using Framework.Module.CMS.Library.Events;
using Framework.Module.CMS.Library.Sport;
using Framework.Module.CMS.Library.Zones;

namespace Framework.Module.CMS.Library.Providers
{
	public class SqlDataProvider : DataAccessProvider
	{
        private string ConnectionString;
        public SqlDataProvider(string connStr)
        {
            this.ConnectionString = connStr;
        }

        #region Zones
        public override int AddZone(ZoneInfo zone)
        {
            return (int)SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Zones_Insert", zone.Zone_Name, zone.Zone_ParentID, zone.Zone_VisibleInLeftBottom, zone.Zone_VisibleInNavbar, zone.Portal_TabControlID, AppEnv.GetLanguage(), zone.Zone_VisibleInRightBottom, zone.Zone_VisibleInTab, zone.Zone_VisibleInTop, zone.Zone_Alias, zone.Zone_Images);
        }
        public override void UpdateZone(ZoneInfo zone)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Zones_Update", zone.Zone_ID, zone.Zone_Name, zone.Zone_ParentID, zone.Zone_Priority, zone.Zone_VisibleInLeftBottom, zone.Zone_VisibleInNavbar, zone.Portal_TabControlID, AppEnv.GetLanguage(), zone.Zone_VisibleInRightBottom, zone.Zone_VisibleInTab, zone.Zone_VisibleInTop, zone.Zone_Alias, zone.Zone_Images);
        }
        public override void DeleteZone(int zoneId)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Zones_Delete", zoneId);
        }
        public override ZoneInfo GetZone(int zoneId)
        {
            IDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, "CMS_Zones_GetInfo", zoneId);
            ZoneInfo zone = (ZoneInfo)ObjectHelper.CreateObject(typeof(ZoneInfo), dr);
            return zone;
        }
        public override DataTable GetZones(int parentId, string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Zones_GetByParentID", parentId, lang);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetZones(int parentId, string portalid, string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Zones_GetByParentIDAndPortalID", parentId, portalid, lang);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetZones(int parentId, int portalid, int typezonevalue, string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Zones_GetByParentIDAndPortalIDAndZoneType", parentId, portalid, typezonevalue, lang);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetZonesVisbleInNavbar(string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Zones_GetZoneVisbleInNavbar", lang, AppEnv.PortalId());
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetZonesVisbleInNavbar(string lang, int portalid)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Zones_GetZoneVisbleInNavbar", lang, portalid);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetZonesVisbleInTab(string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Zones_GetZoneVisbleInTab", lang, AppEnv.PortalId());
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetZonesVisbleInTop(string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Zones_GetZoneVisbleInTop", lang, AppEnv.PortalId());
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetZonesVisbleInRightBottom(string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Zones_GetZoneVisbleInRightBottom", lang, AppEnv.PortalId());
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetZonesVisbleInLeftBottom(string lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Zones_GetZoneVisbleInLeftBottom", lang, AppEnv.PortalId());
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetZonesDeployed(int contentId)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetZoneDeployed", contentId);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override void SetZonePriority(int zoneId, int priority)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Zones_SetPriority", zoneId, priority);
        }
        public override int GetParentID(int zoneid)
        {
            return (int)SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Zones_GetParentID", zoneid);
        }
        public override string GetZoneNameByZoneID(int zoneId)
        {
            object obj = SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Zones_GetZoneNameByID", zoneId);
            return obj != null ? obj.ToString() : string.Empty;
            //IDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, "CMS_Zones_GetZoneNameByID", zoneId);
            //string zonename =dr.GetString(0);// (string)ObjectHelper.CreateObject(typeof(string), dr);
            //return zonename;
        }
        #endregion

        #region CMS Permision
        public override bool CheckUserPermision(int userId, int permision, int portalid)
        {
            int result = (int)SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Permisions_CheckUserPermision", userId, permision, portalid);
            return result > 0;
        }
        public override void UpdateUserPermision(int userId, int permision, string zoneCollection, int portalid)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Permisions_UpdateUserPermision", userId, permision, zoneCollection, portalid);
        }
        public override void RemoveUserPermision(int userId, int portalid)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Permisions_RemoveUserPermision", userId, portalid);
        }
        public override string GetPermisionZones(int userId, int portalid)
        {
            return (string)SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Permisions_GetPermisionZones", userId, portalid);
        }
        public override ZonePermissionInfo GetPermisionInfo(int userId, int portalid)
        {
            IDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, "CMS_Permisions_GetPermision", userId, portalid);
            ZonePermissionInfo objPermision = (ZonePermissionInfo)ObjectHelper.CreateObject(typeof(ZonePermissionInfo), dr);
            return objPermision;
        }
        #endregion

        //Portal
        public override DataTable GetPortalsByLang(string _lang)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "Portal_Portals_GetAllByLang", _lang);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        

        //Content
        public override DataTable GetContentsInContentList(int zoneId, int portalId)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Get_New_In_ContentList", zoneId, portalId);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }

        public override DataTable GetVersionByContentId(int contentId)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Contents_Version_GetVersionByContentId", contentId);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
	    
        public override int AddContentVersion(ContentVersionInfo content)
        {
            return (int)SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Contents_Insert_Version",content.Content_ID, content.Content_Headline, content.Content_Teaser, content.Content_Body, content.Content_Avatar, content.Content_AvatarDescription, content.Content_AvatarWidth, content.Content_AvaterHeight, content.Content_Status, content.Content_ZoneID, content.Content_UserID, content.Content_ModifiedUserID, content.Content_IsComment, content.Content_Alias, content.Content_Source, content.CheckOutByUser, content.DeployedVersion, content.Content_SubTitle, content.Content_NoteBox, content.SpecialAvatar);
        }
        public override int Version_GetVersionUsedNow(int contentid)
        {
            object obj = SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Contents_Version_GetVersionUsedNow", contentid);
            return obj != null ? (int)obj : 0;
        }
        //public override int Version_GetVersionRollback(int contentid)
        //{
        //    return (int)SqlHelper.ExecuteScalar(this.connectionString, "CMS_Contents_Version_GetVersionRollback", contentid);
        //}
        public override int Version_GetVersionRollback(int contentid)
        {

            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Contents_Version_GetVersionRollback", contentid);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return Convert.ToInt32(ds.Tables[0].Rows[0]["DeployedBefore"]);
            }
            return 0;

        }

        public override void UpdateContentVersion(ContentVersionInfo content)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Contents_Update_Version", content.Content_Version_ID, content.Content_ID, content.Content_Headline, content.Content_Teaser, content.Content_Body, content.Content_Avatar, content.Content_AvatarDescription, content.Content_AvatarWidth, content.Content_AvaterHeight, content.Content_Status, content.Content_ZoneID, content.Content_UserID, content.Content_ModifiedUserID, content.Content_IsComment, content.Content_Alias, content.Content_Source, content.CheckOutByUser, content.Content_SubTitle, content.Content_NoteBox, content.SpecialAvatar);
        }
        public override void Version_SetVersionRollback(int versionid, int versionbefore)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Contents_Version_SetVersionRollback", versionid, versionbefore);
        }
        public override ContentVersionInfo GetInfoVersion(int contentVersionID)
        {
            IDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, "CMS_Contents_GetInfoVersion", contentVersionID);
            ContentVersionInfo info = (ContentVersionInfo)ObjectHelper.CreateObject(typeof(ContentVersionInfo), dr);
            return info;
        }
        public override int AddContent(ContentInfo content)
        {
                return (int) SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Contents_Insert",content.Content_Headline, content.Content_Teaser,content.Content_Body,content.Content_Avatar, content.Content_AvatarDescription,content.Content_AvatarWidth,content.Content_AvaterHeight, content.Content_Status,content.Content_ZoneID, content.Content_UserID,content.Content_ModifiedUserID, content.Content_ApproveUser ,content.Content_IsComment, content.Content_Alias, content.Content_Source, content.Event_ID, content.Content_SubTitle, content.Content_NoteBox, content.SetAlbum, content.SpecialAvatar);
        }
        public override DataTable GetContents()
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetAll");
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
    
        //CMS_Distributions_GetContentByView
        //nguyen cho them Lang vao ngay 28/03/2008
        public override DataTable Distributions_GetContentByView(int numberrecord)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetContentByView", numberrecord, ConvertUtility.ToInt32(AppEnv.PortalId()),AppEnv.GetLanguage());
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable Distributions_GetContentByView(int numberrecord, int portalid)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetContentByView", numberrecord, portalid,AppEnv.GetLanguage());
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        //CMS_Distributions_GetNewContent
        public override DataTable Distributions_GetNewContent(int numberrecord)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetNewContent", numberrecord, ConvertUtility.ToInt32(AppEnv.PortalId()));
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
     
        // search for web
        //CMS_Distributions_Search
        public override DataTable SearchForWebAdvance(int pagesize, int curpage, out int totalrecord, string text, int zoneid)
        {
            DataTable retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("CMS_Distributions_SearchAdvance", dbConn);
            dbCmd.Parameters.AddWithValue("@PageSize", pagesize);
            dbCmd.Parameters.AddWithValue("@CurrentPage", curpage);
            dbCmd.Parameters.AddWithValue("@Text", text);
            dbCmd.Parameters.AddWithValue("@Lang", AppEnv.GetLanguage());
            dbCmd.Parameters.AddWithValue("@PortalID", ConvertUtility.ToInt32(AppEnv.PortalId()));
            dbCmd.Parameters.AddWithValue("@ZoneID", zoneid);
            dbCmd.Parameters.Add(new SqlParameter("@TotalRecord", SqlDbType.Int));
            dbCmd.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                try
                {
                    totalrecord = Convert.ToInt32(dbCmd.Parameters["@TotalRecord"].Value);
                }
                catch
                {
                    totalrecord = 0;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public override DataTable SearchForWeb(int pagesize, int curpage, out int totalrecord, string text)
        {
            DataTable retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("CMS_Distributions_Search", dbConn);
            dbCmd.Parameters.AddWithValue("@PageSize", pagesize);
            dbCmd.Parameters.AddWithValue("@CurrentPage", curpage);
            dbCmd.Parameters.AddWithValue("@Text", text);
            dbCmd.Parameters.AddWithValue("@Lang", AppEnv.GetLanguage());
            dbCmd.Parameters.AddWithValue("@PortalID", ConvertUtility.ToInt32(AppEnv.PortalId()));
            dbCmd.Parameters.Add(new SqlParameter("@TotalRecord", SqlDbType.Int));
            dbCmd.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                try
                {
                    totalrecord = Convert.ToInt32(dbCmd.Parameters["@TotalRecord"].Value);
                }
                catch
                {
                    totalrecord = 0;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public override DataTable Search(int pagesize, int distid, out int totalrecord, string text, bool curpage)
        {
            DataTable retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("CMS_Search", dbConn);
            dbCmd.Parameters.AddWithValue("@PageSize", pagesize);
            dbCmd.Parameters.AddWithValue("@Distribution_ID", distid);
            dbCmd.Parameters.AddWithValue("@Text", text);
            dbCmd.Parameters.AddWithValue("@Lang", AppEnv.GetLanguage());
            dbCmd.Parameters.AddWithValue("@PortalID", ConvertUtility.ToInt32(AppEnv.PortalId()));
            dbCmd.Parameters.Add(new SqlParameter("@TotalRecord", SqlDbType.Int));
            dbCmd.Parameters.AddWithValue("@CurPage", curpage);
            dbCmd.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                try
                {
                    totalrecord = Convert.ToInt32(dbCmd.Parameters["@TotalRecord"].Value);
                }
                catch
                {
                    totalrecord = 0;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        
        public override DataTable GetNewContentByZoneIDAndRank(int zoneID, bool inZone, int pageSize, int currentPage, out int totalRecord)
        {
            DataTable retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("CMS_Distributions_GetNewContentByZoneIDAndRank", dbConn);
            dbCmd.Parameters.AddWithValue("@ZoneID", zoneID);
            dbCmd.Parameters.AddWithValue("@InZone", inZone);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.Add(new SqlParameter("@TotalRecord", SqlDbType.Int));
            dbCmd.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                totalRecord = Convert.ToInt32(dbCmd.Parameters["@TotalRecord"].Value);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public override ContentInfo GetInfo(int contentID)
        {
            IDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, "CMS_Contents_GetInfo", contentID);
            ContentInfo info = (ContentInfo)ObjectHelper.CreateObject(typeof(ContentInfo), dr);
            return info;
        }
        public override DataTable GetContents(int zoneId, int status, int userId)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Contents_GetContentsByZoneIDAndUserID", zoneId, status, userId);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetContents(int zoneId, int status)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Contents_GetContentsByZoneID", zoneId, status);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override void DeleteContent(int contentId)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Contents_Delete", contentId);
        }
        public override void DeleteContentVersion(int contentVersionId)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_ContentsVersion_Delete", contentVersionId);
        }
        public override bool RemoverInZoneID(int Distribution_ContentID, int Distribution_ZoneID)
        {
            int result = ConvertUtility.ToInt32( SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Distributions_RemoverInZoneID", Distribution_ContentID, Distribution_ZoneID));
            return result > 0;
        }
        public override void UpdatePostDate(DistributionInfo _distinfo)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Distribution_UpdatePostDate", _distinfo.Distribution_ID, _distinfo.Distribution_WaittingDate, _distinfo.Distribution_WaittingHour, _distinfo.HotNewsInHomePage, _distinfo.RightPositionInLevel2);
        }
        public override void UpdateContent(ContentInfo content)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Contents_Update", content.Content_ID, content.Content_Headline, content.Content_Teaser, content.Content_Body, content.Content_Avatar, content.Content_AvatarDescription, content.Content_AvatarWidth, content.Content_AvaterHeight, content.Content_Status, content.Content_ZoneID, content.Content_UserID, content.Content_ModifiedUserID, content.Content_ApproveUser, content.Content_IsComment, content.Content_Alias, content.Content_Source, content.CheckInByUser, content.Event_ID, content.Content_SubTitle, content.Content_NoteBox, content.SetAlbum, content.SpecialAvatar);
        }
      
        public override void SetContentComment(int contentId, string comment)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Contents_SetComment", contentId, comment);
        }
		
        public override ContentInfo GetContent(int contentId)
        {
            IDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, "CMS_Contents_GetInfo", contentId);
            ContentInfo content = (ContentInfo)ObjectHelper.CreateObject(typeof(ContentInfo), dr);
            return content;
        }
       
        public override void SetContentStatus(int contentId, int status)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Contents_SetStatus", contentId, status);
        }
       
        public override void LockContent(int contentId, int userId)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Contents_LockContent", contentId, userId);
        }
        public override void UnLockContent(int contentId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "CMS_Contents_UnLockContent", contentId);
        }
		//Distribution

		public override int AddDistribution(DistributionInfo distribution)
		{
			return ConvertUtility.ToInt32(SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Distributions_Insert", distribution.Distribution_ContentID,
                            distribution.Distribution_ZoneID, distribution.Distribution_Rank, distribution.Distribution_ExpireDate, distribution.Distribution_WaittingDate, distribution.Distribution_WaittingHour, distribution.Distribution_View, ConvertUtility.ToInt32(AppEnv.PortalId()), distribution.HotNewsInHomePage, distribution.RightPositionInLevel2));
		}

        public override int GetDistID(int ContentID)
        {
            return ConvertUtility.ToInt32(SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Distributions_GetDistIDByContentID", ContentID));
        }

		public override void SetDistributionPriority(int distId, int priority)
		{
			SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Distributions_SetPriority", distId, priority);
		}
		public override void SetDistributionRank(int distId, int rank)
		{
			SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Distributions_SetRank", distId, rank);
		}
		public override void UpdateDistributionView(int distId, int number)
		{
			SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Distributions_UpdateView", distId, number, ConvertUtility.ToInt32(AppEnv.PortalId()));
		}
		public override void DeleteDistribution(int distId)
		{
			SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Distributions_Delete", distId);
		}
		public override void DeleteDistribution(int contentId, int zoneId)
		{
			SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Distributions_DeleteInZone", contentId, zoneId);
		}
		public override DistributionInfo GetDistribution(int distId)
		{
			IDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, "CMS_Distributions_GetInfo", distId);
			DistributionInfo distribution = (DistributionInfo)ObjectHelper.CreateObject(typeof(DistributionInfo), dr);
			return distribution;
		}
      
		public override DataTable GetDistributedContents(int zoneId, int top, int rank)
		{
			DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetContentsByZoneIDAndRank", zoneId, top, rank);
			if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
			return null;
		}

        public override DataTable GetDistributedContentsByZone(int zoneId)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetContentsByZone", zoneId);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }

        public override DataTable GetDistributedRightContents(int zoneId, int top, bool bol)
		{
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetRightContentsByZoneIDAndRank", zoneId, top, bol);
			if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
			return null;
		}

        public override DataTable GetDistributedHotNewsContents(int zoneId, int top)
		{
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetHotNewsContent", zoneId, top);
			if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
			return null;
		}

        public override DataTable GetDistributedHotNewsContentsbyPortal(int portalid, int top)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetHotNewsContentByPortalID", portalid, top);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
	    
        public override DataTable GetDistributedContents(int zoneId, int top, int rank, DateTime date)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetContentsByZoneIDAndRankDate", zoneId, top, rank, date);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        //CMS_Distributions_GetContentsBySeftZoneIDAndRank
        public override DataTable GetContentsBySeftZoneIDAndRank(int zoneId, int top, int rank)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetContentsBySeftZoneIDAndRank", zoneId, top, rank);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetDistributedContents(int zoneId, int userid)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetDeploysByZoneIDAndUserID", zoneId, userid);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable GetDistributedContents(int zoneId)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetDeploysByZoneID", zoneId);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
		public override DataTable GetOldDistributedContent(int curContentId, int numberRecord)
		{
			DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetPreviousForCurrent", curContentId, numberRecord);
			if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
			return null;
		}
		public override DataTable GetNewDistributedContent(int curContentId, int numberRecord)
		{
			DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Distributions_GetNewsForCurrent", curContentId, numberRecord);
			if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
			return null;
		}

        public override DataTable EventGetAll()
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Events_GetAll", AppEnv.GetLanguage(), ConvertUtility.ToInt32(AppEnv.PortalId()));
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable EventGetAllByNum(int num)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Events_GetAllByNum", AppEnv.GetLanguage(), ConvertUtility.ToInt32(AppEnv.PortalId()), num);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
	    
        public override void Event_SetDistributionPriority(int eventid, int distid, int newPriority)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Events_SetDistributionPriority", newPriority, eventid, distid);
            
        }

        public override void SetPriority(int eventid, int newPriority)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Events_SetPriority", newPriority, eventid);

        }
        public override int Event_GetEventIDByDistID(int distid)
        {
            return ConvertUtility.ToInt32(SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Events_GetEventIDByDistID", distid));
        }
        public override DataTable Event_GetNewsByEventID(int eventid, int numberRecord)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Events_GetNewsByEventID", eventid, numberRecord);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override DataTable Event_GetNewsByEventIDExcludeDistID(int eventid, int numberRecord, int distid)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "CMS_Events_GetNewsByEventIDExcludeDistId", eventid, numberRecord, distid);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }
        public override EventInfo Event_GetInfo(int eventid)
        {
            IDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, "CMS_Events_GetInfo", eventid);
            EventInfo objevent = (EventInfo)ObjectHelper.CreateObject(typeof(EventInfo), dr);
            return objevent;
        }
        public override EventInfo Event_GetEventInfoByDistID(int distid)
        {
            return Event_GetInfo(Event_GetEventIDByDistID(distid));
        }
        public override void Event_AddDistribution(int eventID, int distid)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Events_AddDistribution", eventID, distid);            
        }
        public override void Event_RemoveDistribution(int eventID, int distid)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Events_RemoveDistribution", eventID, distid);
        }
        public override void Event_Delete(int eventID)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Events_Delete", eventID);
        }
        public override int Event_Insert(EventInfo einfo)
        {
            return (int)SqlHelper.ExecuteScalar(this.ConnectionString, "CMS_Events_Insert", einfo.Event_Name, einfo.Event_Description,einfo.Event_Avatar, einfo.Event_CreateDate, einfo.Event_Lang, einfo.PortalID);
        }
        public override void Event_Update(EventInfo einfo)
        {
            SqlHelper.ExecuteNonQuery(this.ConnectionString, "CMS_Events_Update", einfo.Event_ID, einfo.Event_Name, einfo.Event_Description, einfo.Event_Avatar, einfo.Event_CreateDate, einfo.Event_Lang, einfo.PortalID);
        }
	    
	    //lay anh tu album theo contentid
        public override DataTable Distributions_GetPicInAlbumByContentId(int contentid)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "Gallery_Picture_GetAllByAlbumIDAndVisibleAndContentId", contentid);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        } 
        // Sport
#region Sport: SoccerSeason
        public override int SoccerSeason_Insert(SoccerSeasonInfo season)
        {
            return ConvertUtility.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, "SoccerSeason_Insert", season.Season_Name, season.Priority, season.Status, season.Portal_ID, season.Create_UserID, season.Modifier_UserID, season.ForYear));
        }

        public override void SoccerSeason_Delete(int season_ID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "SoccerSeason_Delete", season_ID);
        }

        public override SoccerSeasonInfo SoccerSeason_GetInfo(int season_id)
        {
            IDataReader dr = SqlHelper.ExecuteReader(ConnectionString, "SoccerSeason_GetInfo", season_id);
            SoccerSeasonInfo si = (SoccerSeasonInfo)ObjectHelper.CreateObject(typeof(SoccerSeasonInfo), dr);
            return si;            
        }
        public override DataTable SoccerSeason_GetAll(bool status)
        {
            DataSet ds = SqlHelper.ExecuteDataset(ConnectionString, "SoccerSeason_GetAll", ConvertUtility.ToInt32(AppEnv.PortalId()), status);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            else
                return null;
            
        }
        public override void SoccerSeason_Update(SoccerSeasonInfo si)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "SoccerSeason_Update", si.Season_ID, si.Season_Name, si.Priority, si.Status, si.Modifier_UserID, si.Modifier_Date, si.ForYear);
        }
        public override void SoccerSeason_SetPriority(int seasonid, int index)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "SoccerSeason_SetIndex", seasonid, index);
        }
        public override void SoccerSeason_SetStatus(int seasonid, bool status)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "SoccerSeason_SetStatus", seasonid, status);
        }
#endregion

#region Sport: Match Schedule
        public override int Match_Insert(MatchSchedule ms)
        {
            return ConvertUtility.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, "MatchSchedule_Insert", ms.Match_Name, ms.Create_UserID, ms.Modify_UserID, ms.Result_Match, ms.Priority, ms.Status, ms.Season_ID, ms.Match_Time));
        }

        public override void Match_Delete(int matchid)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "MatchSchedule_Delete", matchid);
        }

        public override MatchSchedule Match_GetInfo(int matchid)
        {
            IDataReader dr = SqlHelper.ExecuteReader(ConnectionString, "MatchSchedule_GetInfo", matchid);
            MatchSchedule si = (MatchSchedule)ObjectHelper.CreateObject(typeof(MatchSchedule), dr);
            return si;
        }
        public override DataTable Match_GetAll(int seasonid, bool status)
        {
            DataSet ds = SqlHelper.ExecuteDataset(ConnectionString, "MatchSchedule_GetAll", seasonid, status);
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            else
                return null;

        }
        public override void Match_Update(MatchSchedule ms)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "MatchSchedule_Update", ms.Match_ID, ms.Match_Name, ms.Modify_UserID, ms.Result_Match, ms.Priority, ms.Status, ms.Season_ID, ms.Match_Time);
        }
        public override void MatchSchedule_SetPriority(int matchid, int index)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "MatchSchedule_SetIndex", matchid, index);
        }
        public override void MatchSchedule_SetStatus(int matchid, bool status)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "MatchSchedule_SetStatus", matchid, status);
        }
        #endregion

     // dungbeo thuc hien search cho admin new
        public override DataTable Distributions_Search(int zoneid, int userid, string text, string lang, int portalid, DateTime fromdate, DateTime todate, string rank)
        {
            DataTable retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("CMS_Distributions_GetDeploysByZoneIDAndUserID1", dbConn);
            dbCmd.Parameters.AddWithValue("@ZoneID", zoneid);
            dbCmd.Parameters.AddWithValue("@UserID", userid);
            dbCmd.Parameters.AddWithValue("@Text", text);
            dbCmd.Parameters.AddWithValue("@Lang", AppEnv.GetLanguage());
            dbCmd.Parameters.AddWithValue("@PortalID", ConvertUtility.ToInt32(AppEnv.PortalId()));
            dbCmd.Parameters.AddWithValue("@FromDate", fromdate);
            dbCmd.Parameters.AddWithValue("@ToDate", todate);
            dbCmd.Parameters.AddWithValue("@Distribution_Rank", rank);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
